package com.gopay.solution.service.manage.impl;

import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.solution.SolutionTypeConstant;
import com.gopay.common.constants.trans.TranSpCustType;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.core.dic.dao.CpsTransInfoQueryDAO;
import com.gopay.common.cps.dao.solution.CpsAcctProdMastQueryDAO;
import com.gopay.common.domain.cps.CpsAcctProdMast;
import com.gopay.common.domain.cps.CpsTransInfo;
import com.gopay.common.exception.validator.GopayCheckException;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.solution.vo.FeeDtlInfo;
import com.gopay.remote.solution.vo.GainDtlInfo;
import com.gopay.remote.solution.vo.PayChannelDtlInfo;
import com.gopay.remote.solution.vo.ProfitDtlInfo;
import com.gopay.remote.solution.vo.SettleDtlInfo;
import com.gopay.remote.solution.vo.SolutionDtlInfo;
import com.gopay.remote.solution.vo.SsApSolution;
import com.gopay.remote.solution.vo.SsComConfInfo;
import com.gopay.remote.solution.vo.SsPlTranSolution;
import com.gopay.remote.solution.vo.SsRskConfInfo;
import com.gopay.remote.solution.vo.SsTranBasicSolution;
import com.gopay.remote.solution.vo.SsTranLegalObjSolution;
import com.gopay.remote.solution.vo.SsTransLimitConfInfo;
import com.gopay.remote.solution.vo.TranBasicDtlInfo;
import com.gopay.remote.solution.vo.TransDtlInfo;
import com.gopay.remote.solution.vo.UIConfigDtlInfo;
import com.gopay.solution.common.constant.OprLevel;
import com.gopay.solution.dao.SolutionRuleDtlDAO;
import com.gopay.solution.dao.SsApSolutionRelDAO;
import com.gopay.solution.dao.SsPlSolutionRelDAO;
import com.gopay.solution.domain.po.SolutionMeta;
import com.gopay.solution.domain.po.SolutionRuleDtl;
import com.gopay.solution.domain.po.SsAcctGainDtl;
import com.gopay.solution.domain.po.SsAcctGainsSolution;
import com.gopay.solution.domain.po.SsApSolutionRel;
import com.gopay.solution.domain.po.SsCapSolutionRel;
import com.gopay.solution.domain.po.SsGpComConfDtl;
import com.gopay.solution.domain.po.SsGpComSolution;
import com.gopay.solution.domain.po.SsGpRiskFundConfDtl;
import com.gopay.solution.domain.po.SsGpRiskFundConfSolution;
import com.gopay.solution.domain.po.SsPlSolutionRel;
import com.gopay.solution.domain.po.SsProdLineTransDtl;
import com.gopay.solution.domain.po.SsTransBasicDtl;
import com.gopay.solution.domain.po.SsTransBasicSolution;
import com.gopay.solution.domain.po.SsTransDtl;
import com.gopay.solution.domain.po.SsTransFeeDtl;
import com.gopay.solution.domain.po.SsTransFeeSolution;
import com.gopay.solution.domain.po.SsTransLegalObjDtl;
import com.gopay.solution.domain.po.SsTransLegalObjSolution;
import com.gopay.solution.domain.po.SsTransLimitConfDtl;
import com.gopay.solution.domain.po.SsTransLimitConfSolution;
import com.gopay.solution.domain.po.SsTransPayChanDtl;
import com.gopay.solution.domain.po.SsTransPayChanSolution;
import com.gopay.solution.domain.po.SsTransProfitDtl;
import com.gopay.solution.domain.po.SsTransProfitSolution;
import com.gopay.solution.domain.po.SsTransSettleDtl;
import com.gopay.solution.domain.po.SsTransSettleSolution;
import com.gopay.solution.domain.po.SsTransSolution;
import com.gopay.solution.domain.po.SsUIConfigDtl;
import com.gopay.solution.domain.po.SsUIConfigSolution;
import com.gopay.solution.domain.vo.Solution;
import com.gopay.solution.manager.SolutionManager;
import com.gopay.solution.manager.SsApSolutionManager;
import com.gopay.solution.manager.SsCapSolutionManager;
import com.gopay.solution.manager.SsLoSolutionManager;
import com.gopay.solution.manager.SsPlSolutionManager;
import com.gopay.solution.manager.SsTbSolutionManager;
import com.gopay.solution.service.manage.SolutionManageService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 方案管理实现
 * 
 * @SolutionManageServiceImpl.java
 * @author liujy
 * @2013-4-7 下午1:49:27 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("solutionManageService")
public class SolutionManageServiceImpl implements SolutionManageService {

	// private Logger logger =
	// LoggerFactory.getLogger(SolutionManageServiceImpl.class);

	/** 校验数据使用，欢总校验位置及是否要校验 */
	@Resource(name = "cpsAcctProdMastQueryDAO")
	private CpsAcctProdMastQueryDAO cpsAcctProdMastQueryDAO;
	@Resource(name = "cpsTransInfoQueryDAO")
	private CpsTransInfoQueryDAO cpsTransInfoQueryDAO;

	@Resource(name = "ssApSolutionRelDAO")
	private SsApSolutionRelDAO ssApSolutionRelDAO;

	/** 校验数据使用，欢总校验位置及是否要校验 */

	@Resource(name = "ssApSolutionManager")
	private SsApSolutionManager ssApSolutionManager;

	@Resource(name = "ssPlSolutionRelDAO")
	private SsPlSolutionRelDAO ssPlSolutionRelDAO;
	@Resource(name = "ssProdLineTransSoluManager")
	private SolutionManager ssProdLineTransSoluManager;

	@Resource(name = "ssTransBasicSoluManager")
	private SolutionManager ssTransBasicSoluManager;

	@Resource(name = "ssTransLegalObjSoluManager")
	private SolutionManager ssTransLegalObjSoluManager;

	@Resource(name = "ssPlSolutionManager")
	private SsPlSolutionManager ssPlSolutionManager;

	@Resource(name = "ssTbSolutionManager")
	private SsTbSolutionManager ssTbSolutionManager;

	@Resource(name = "ssLoSolutionManager")
	private SsLoSolutionManager ssLoSolutionManager;

	@Resource(name = "ssCapSolutionManager")
	private SsCapSolutionManager sCapSolutionManager;

	@Resource(name = "ssTransLimitConfDtlDAO")
	private SolutionRuleDtlDAO ssTransLimitConfDtlDAO;

	@Resource(name = "ssTransLimitConfSoluManager")
	private SolutionManager ssTransLimitConfSoluManager;

	/**
	 * 查询账户产品交易信息
	 */
	public List<SsApSolution> findAllAPTransSolution() {

		// 获得所有账户产品的交易(TRN)方案
		List<SsApSolutionRel> ssApSolutionRels = ssApSolutionManager
				.findAllRelByTrn();
		if (ssApSolutionRels == null || ssApSolutionRels.isEmpty()) {
			return null;
		}

		List<SsApSolution> list = new ArrayList<SsApSolution>();

		for (int i = 0, size = ssApSolutionRels.size(); i < size; i++) {
			SsApSolutionRel rel = ssApSolutionRels.get(i);
			SsApSolution aps = initSsApSolution(rel);
			setSsApSolutionDtlInfos(aps, SolutionTypeConstant.TRANS_TRN);
			list.add(aps);
		}
		return list;
	}

	@Override
	public List<SsApSolution> queryActLimit(String custId) {
		List<SsCapSolutionRel> ssCapSolutionRels = sCapSolutionManager
				.findRelByCustId(custId, SolutionTypeConstant.CUST_TLCS);
		List<SsApSolution> list = new ArrayList<SsApSolution>();
		if (ssCapSolutionRels == null || ssCapSolutionRels.isEmpty()) {
			return null;
		} else {
			for (int i = 0, size = ssCapSolutionRels.size(); i < size; i++) {
				SsCapSolutionRel rel = ssCapSolutionRels.get(i);
				SsApSolution aps = initSsCapSolution(rel);
				setSsApSolutionDtlInfos(aps, SolutionTypeConstant.CUST_TLCS);
				list.add(aps);
			}
		}
		return list;
	}

	@Override
	public List<SsApSolution> queryAccountLimit(String custId) {
		List<SsCapSolutionRel> ssCapSolutionRels = sCapSolutionManager
				.findRelByCustId(custId, SolutionTypeConstant.CUST_TLCS);
		List<SsApSolution> list = new ArrayList<SsApSolution>();
		if (ssCapSolutionRels == null || ssCapSolutionRels.isEmpty()) {
			// 获得所有账户产品的额度管理(TLCS)方案
			List<SsApSolutionRel> ssApSolutionRels = ssApSolutionManager
					.findRelBySolutionType(SolutionTypeConstant.CUST_TLCS);

			for (int i = 0, size = ssApSolutionRels.size(); i < size; i++) {
				SsApSolutionRel rel = ssApSolutionRels.get(i);
				SsApSolution aps = initSsApSolution(rel);
				setSsApSolutionDtlInfos(aps, SolutionTypeConstant.CUST_TLCS);
				list.add(aps);
			}
		} else {
			for (int i = 0, size = ssCapSolutionRels.size(); i < size; i++) {
				SsCapSolutionRel rel = ssCapSolutionRels.get(i);
				SsApSolution aps = initSsCapSolution(rel);
				list.add(aps);
			}
		}
		return list;
	}

	private SsApSolution initSsCapSolution(SsCapSolutionRel rel) {
		if (null == rel) {
			return null;
		}

		SsApSolution aps = new SsApSolution();
		aps.setProdCode(rel.getProdCode());
		aps.setProdName("");
		aps.setSsApSolutionRelSq("" + rel.getSq());
		aps.setSolutionType(rel.getSolutionType());
		aps.setSolutionId("" + rel.getSolutionId());

		return aps;
	}

	@Override
	public List<SsApSolution> queryAccountProLimit() {
		// 获得所有账户产品的额度管理(TLCS)方案
		List<SsApSolutionRel> ssApSolutionRels = ssApSolutionManager
				.findRelBySolutionType(SolutionTypeConstant.CUST_TLCS);
		List<SsApSolution> list = new ArrayList<SsApSolution>();

		if (ssApSolutionRels != null) {
			for (int i = 0, size = ssApSolutionRels.size(); i < size; i++) {
				SsApSolutionRel rel = ssApSolutionRels.get(i);
				SsApSolution aps = initSsApSolution(rel);
				setSsApSolutionDtlInfos(aps, SolutionTypeConstant.CUST_TLCS);
				list.add(aps);
			}
		}

		return list;
	}

	@Override
	public SsApSolution queryProLimitByProdCode(String prodCode) {
		// 获得所有账户产品的额度管理(TLCS)方案
		SsApSolutionRel ssApSolutionRel = ssApSolutionManager
				.findRelByProdCode(prodCode, SolutionTypeConstant.CUST_TLCS);
		if (ssApSolutionRel != null) {
			SsApSolution ssApSolution = initSsApSolution(ssApSolutionRel);
			setSsApSolutionDtlInfos(ssApSolution,
					SolutionTypeConstant.CUST_TLCS);
			return ssApSolution;
		}
		return null;
	}

	@Override
	public SsApSolution queryAccountProLimitBySoluId(String solutionId) {
		// 获得所有账户产品的额度管理(TLCS)方案
		SsApSolutionRel ssApSolutionRels = ssApSolutionManager
				.findRelBySoluIdAndActType(solutionId,
						SolutionTypeConstant.CUST_TLCS);
		SsApSolution aps = initSsApSolution(ssApSolutionRels);
		setSsApSolutionDtlInfos(aps, SolutionTypeConstant.CUST_TLCS);
		return aps;
	}

	@Override
	public SsApSolution findAPTransSolution(String acctProdCode) {
		SsApSolutionRel rel = ssApSolutionManager.findRelByProdCode(
				acctProdCode, SolutionTypeConstant.TRANS_TRN);
		if (null == rel) {
			return null;
		}
		SsApSolution aps = initSsApSolution(rel);
		setSsApSolutionDtlInfos(aps, rel.getSolutionType());
		return aps;
	}

	@Override
	public SsApSolution findAPGainSolution(String acctProdCode) {
		SsApSolutionRel rel = ssApSolutionManager.findRelByProdCode(
				acctProdCode, SolutionTypeConstant.ACCT_GAIN);
		if (null == rel) {
			return null;
		}
		SsApSolution aps = initSsApSolution(rel);
		setSsApSolutionDtlInfos(aps, rel.getSolutionType());
		return aps;
	}

	@Override
	public SsApSolution findAPUIConfigSolution(String acctProdCode) {
		SsApSolutionRel rel = ssApSolutionManager.findRelByProdCode(
				acctProdCode, SolutionTypeConstant.TRANS_UIS);
		if (null == rel) {
			return null;
		}
		SsApSolution aps = initSsApSolution(rel);
		setSsApSolutionDtlInfos(aps, rel.getSolutionType());
		return aps;
	}

	@Override
	public SsApSolution findAPFeeSolution(String acctProdCode,
			String gopayIntTxnCd) {
		SsApSolutionRel rel = ssApSolutionManager.findRelByProdCode(
				acctProdCode, SolutionTypeConstant.TRANS_FEE);
		if (null == rel) {
			return null;
		}
		SsApSolution aps = initSsApSolution(rel);
		setSsApSolutionDtlInfos(aps, rel.getSolutionType(), gopayIntTxnCd);
		return aps;
	}

	@Override
	public SsApSolution findAPPayChannSolution(String acctProdCode,
			String gopayIntTxnCd) {
		SsApSolutionRel rel = ssApSolutionManager.findRelByProdCode(
				acctProdCode, SolutionTypeConstant.TRANS_PAYCHAN);
		if (null == rel) {
			return null;
		}
		SsApSolution aps = initSsApSolution(rel);
		setSsApSolutionDtlInfos(aps, rel.getSolutionType(), gopayIntTxnCd);
		return aps;
	}

	@Override
	public String modifyApGainSolution(SsApSolution ssApSolution) {
		return modifyProdSolution(ssApSolution, SolutionTypeConstant.ACCT_GAIN);
	}

	@Override
	public String modifyApUIConfigSolution(SsApSolution ssApSolution) {
		return modifyProdSolution(ssApSolution, SolutionTypeConstant.TRANS_UIS);
	}

	@Override
	public String modifyApTransSolution(SsApSolution ssApSolution) {
		return modifyProdSolution(ssApSolution, SolutionTypeConstant.TRANS_TRN);
	}

	@Override
	public String modifyApPayChannSolution(SsApSolution ssApSolution,
			String gopayIntTxnCd) {
		String result = modifyProdSolution(ssApSolution, gopayIntTxnCd,
				SolutionTypeConstant.TRANS_PAYCHAN);
		return result;
	}

	@Override
	public String modifyApFeeSolution(SsApSolution ssApSolution,
			String gopayIntTxnCd) {
		return modifyProdSolution(ssApSolution, gopayIntTxnCd,
				SolutionTypeConstant.TRANS_FEE);
	}

	@Override
	public String modifyAccountProLimit(SsApSolution ssApSolution) {
		return modifyLimitProdSolution(ssApSolution,
				SolutionTypeConstant.CUST_TLCS);
	}

	@Override
	public String modifyAccountLimit(SsApSolution ssApSolution, String custId) {
		return modifLimitActSolution(ssApSolution, custId,
				SolutionTypeConstant.CUST_TLCS);
	}

	/**
	 * 
	 * @param ssApSolution
	 * @param solutionType
	 * @return
	 */
	private String modifyLimitProdSolution(SsApSolution ssApSolution,
			String solutionType) {

		if (ssApSolution == null) {
			return "账户产品方案设置：数据为空";
		}

		String acctProdCode = ssApSolution.getProdCode();

		String error = checkProdCode(acctProdCode);
		if (null != error) {
			return error;
		}

		List<SolutionDtlInfo> transDtlInfos = ssApSolution.getSolutionDtls();

		// 查询账户产品关系记录
		SsApSolutionRel rel = ssApSolutionManager.findRelByProdCode(
				acctProdCode, solutionType);

		// 如果请求参数为空，则表示删除此关系记录、方案记录及明细
		if (transDtlInfos == null || transDtlInfos.isEmpty()) {
			// 1: delete rel, delete solution, delete transDtl
			ssApSolutionManager.deleteRelAndChild(rel, solutionType);
			return ProcCodeConstants.PROC_CODE_200S1000;
		}

		// 如果请求参数存在有效数据，但关系表不存在对应记录，则新建关系记录，并保存新方案及明细信息
		if (rel == null) {
			// 2: save rel, save solution, save transDtl
			rel = createApSolRel(acctProdCode, solutionType);
			Solution s = new Solution();
			SolutionMeta meta = createSolutionMeta(solutionType);
			List<SolutionRuleDtl> dlts = createRuleDtls(transDtlInfos, null,
					solutionType, meta.getSolutionId());
			s.setMeta(meta);
			s.setRuleDtls(dlts);
			ssApSolutionManager.saveRelAndChild(rel, s);
			return ProcCodeConstants.PROC_CODE_200S1000;
		}

		// 3：solution存在，但明细不存在情况不会出现

		// 方案及明细均存在，先删除旧的明细信息，再保存新的明细信息
		Solution s = ssApSolutionManager
				.load(rel.getSolutionId(), solutionType);
		if (null != s) {
			// 4-5: delete solutin and dtl, save solution and dtl
			List<SolutionRuleDtl> dlts = createRuleDtls(transDtlInfos, null,
					solutionType, s.getMeta().getSolutionId());
			s.setRuleDtls(dlts);
			ssApSolutionManager.updateSolutionChild(s, solutionType);

			return ProcCodeConstants.PROC_CODE_200S1000;
		}

		return "方案不存在，无法操作！产品代码：" + acctProdCode;
	}

	@Override
	public SsApSolution queryActLimitByProdCode(String custId, String prodCode,
			String today) {
		SsCapSolutionRel ssCapSolutionRel = sCapSolutionManager
				.findRelByCustId(custId, prodCode,
						SolutionTypeConstant.CUST_TLCS);
		SsApSolution s = new SsApSolution();
		if (ssCapSolutionRel != null) {
			try {
				BeanUtils.copyProperties(s, ssCapSolutionRel);
				setSsApSolutionDtlInfos(s, SolutionTypeConstant.CUST_TLCS);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}

			try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
				Date day = sdf.parse(today);

				Date EndDate = sdf.parse(s.getEndDate()); // 判断方案是否过期
				
				if (EndDate.getTime() < day.getTime()) {
					return getSsApSolution(s, ssCapSolutionRel, prodCode);
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			

			return s;
		} else {
			return getSsApSolution(s, ssCapSolutionRel, prodCode);
		}

	}

	private SsApSolution getSsApSolution(SsApSolution s,
			SsCapSolutionRel ssCapSolutionRel, String prodCode) {
		SsApSolutionRel sRel = ssApSolutionManager.findRelByProdCode(prodCode,
				SolutionTypeConstant.CUST_TLCS);
		if (sRel == null) {
			return null;
		}
		try {
			BeanUtils.copyProperties(s, sRel);
			setSsApSolutionDtlInfos(s, SolutionTypeConstant.CUST_TLCS);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return s;
	}

	/**
	 * 
	 * @param ssApSolution
	 * @param solutionType
	 */
	private String modifLimitActSolution(SsApSolution ssApSolution,
			String custId, String solutionType) {

		if (ssApSolution == null) {
			return "方案设置：数据为空." + solutionType;
		}
		String acctProdCode = ssApSolution.getProdCode();
		String error = checkProdCode(acctProdCode);
		if (null != error) {
			return error;
		}

		SsCapSolutionRel rel = sCapSolutionManager.findRelByCustId(custId,
				acctProdCode, solutionType);

		List<SolutionDtlInfo> dtlInfos = ssApSolution.getSolutionDtls();

		// 如果请求参数为空，则表示删除此关系对应方案下特定交易码的明细记录
		if (dtlInfos == null || dtlInfos.isEmpty()) {
			// 1: delete Dtl(inttxncd=gopayIntTxnCd)
			sCapSolutionManager.deleteRelAndChild(rel, solutionType);

			return ProcCodeConstants.PROC_CODE_200S1000;
		}

		Map<String, Object> attrs = new HashMap<String, Object>();

		// 如果请求参数存在有效数据，但关系表不存在对应记录，则新建关系记录，并保存新方案及明细信息
		if (rel == null) {
			// 2: save rel, save solution, save transDtl
			rel = createCapSolRel(acctProdCode, solutionType, custId);
			rel.setSolutionId(System.currentTimeMillis());
			Solution s = new Solution();
			SolutionMeta meta = createSolutionMeta(solutionType);
			meta.setEndDate(ssApSolution.getEndDate());
			meta.setStartDate(ssApSolution.getStartDate());
			List<SolutionRuleDtl> dlts = createRuleDtls(dtlInfos, attrs,
					solutionType, meta.getSolutionId());
			s.setMeta(meta);
			s.setRuleDtls(dlts);
			sCapSolutionManager.saveRelAndChild(rel, s);
			return ProcCodeConstants.PROC_CODE_200S1000;
		}

		// 方案及明细均存在，先删除旧的明细信息，再保存新的明细信息
		Solution s = ssApSolutionManager
				.load(rel.getSolutionId(), solutionType);
		if (s != null) {
			s.getMeta().setEndDate(ssApSolution.getEndDate());
			s.getMeta().setStartDate(ssApSolution.getStartDate());
			// 新的dtl赋值到solution中
			List<SolutionRuleDtl> dlts = createRuleDtls(dtlInfos, attrs,
					solutionType, s.getMeta().getSolutionId());
			s.setRuleDtls(dlts);
			sCapSolutionManager.updateSolutionChild(s, solutionType);
			return ProcCodeConstants.PROC_CODE_200S1000;
		}
		return "方案不存在，无法操作！产品代码：" + acctProdCode;
	}

	/**
	 * 
	 * @param ssApSolution
	 * @param solutionType
	 * @return
	 */
	private String modifyProdSolution(SsApSolution ssApSolution,
			String solutionType) {

		if (ssApSolution == null) {
			return "账户产品方案设置：数据为空";
		}

		String acctProdCode = ssApSolution.getProdCode();

		String error = checkProdCode(acctProdCode);
		if (null != error) {
			return error;
		}

		List<SolutionDtlInfo> transDtlInfos = ssApSolution.getSolutionDtls();
		if (SolutionTypeConstant.TRANS_TRN.equals(solutionType)) {
			for (int i = 0, size = transDtlInfos == null ? 0 : transDtlInfos
					.size(); i < size; i++) {
				String intTxnCd = ((TransDtlInfo) transDtlInfos.get(i))
						.getGopayIntTxnCd();
				error = checkIntTxnCds(intTxnCd, solutionType);
				if (null != error) {
					return error;
				}
			}
		}

		// 查询账户产品关系记录
		SsApSolutionRel rel = ssApSolutionManager.findRelByProdCode(
				acctProdCode, solutionType);

		// 如果请求参数为空，则表示删除此关系记录、方案记录及明细
		if (transDtlInfos == null || transDtlInfos.isEmpty()) {
			// 1: delete rel, delete solution, delete transDtl
			ssApSolutionManager.deleteRelAndChild(rel, solutionType);
			return ProcCodeConstants.PROC_CODE_000S1000;
		}

		// 如果请求参数存在有效数据，但关系表不存在对应记录，则新建关系记录，并保存新方案及明细信息
		if (rel == null) {
			// 2: save rel, save solution, save transDtl
			rel = createApSolRel(acctProdCode, solutionType);
			Solution s = new Solution();
			SolutionMeta meta = createSolutionMeta(solutionType);
			List<SolutionRuleDtl> dlts = createRuleDtls(transDtlInfos, null,
					solutionType, meta.getSolutionId());
			s.setMeta(meta);
			s.setRuleDtls(dlts);
			ssApSolutionManager.saveRelAndChild(rel, s);
			return ProcCodeConstants.PROC_CODE_000S1000;
		}

		// 3：solution存在，但明细不存在情况不会出现

		// 方案及明细均存在，先删除旧的明细信息，再保存新的明细信息
		Solution s = ssApSolutionManager
				.load(rel.getSolutionId(), solutionType);
		if (null != s) {
			// 4-5: delete solutin and dtl, save solution and dtl
			List<SolutionRuleDtl> dlts = createRuleDtls(transDtlInfos, null,
					solutionType, s.getMeta().getSolutionId());
			s.setRuleDtls(dlts);
			ssApSolutionManager.updateSolutionChild(s, solutionType);

			return ProcCodeConstants.PROC_CODE_000S1000;
		}

		return "方案不存在，无法操作！产品代码：" + acctProdCode;
	}

	/**
	 * 
	 * @param ssApSolution
	 * @param gopayIntTxnCd
	 */
	private String modifyProdSolution(SsApSolution ssApSolution,
			String gopayIntTxnCd, String solutionType) {

		if (ssApSolution == null) {
			return "方案设置：数据为空." + solutionType;
		}
		String acctProdCode = ssApSolution.getProdCode();
		String error = checkProdCode(acctProdCode);
		if (null != error) {
			return error;
		}

		error = checkIntTxnCds(gopayIntTxnCd, solutionType);
		if (null != error) {
			return error;
		}

		SsApSolutionRel rel = ssApSolutionManager.findRelByProdCode(
				acctProdCode, solutionType);

		List<SolutionDtlInfo> dtlInfos = ssApSolution.getSolutionDtls();

		// 如果请求参数为空，则表示删除此关系对应方案下特定交易码的明细记录
		if (dtlInfos == null || dtlInfos.isEmpty()) {
			//ssApSolutionManager.deleteRelAndChild(rel, solutionType);

		    //add by fanghw 20151222
		    //以清算渠道举例：只删除ss_trans_pay_chan_dtl表中的本交易码明细，不删除SS_AP_SOLUTION_REL表和ss_trans_pay_chan_solution表
			//update by wangdong 20160902传入账户类型，缓存使用
		    ssApSolutionManager.delBySoluIdAndIntTxnCd(rel.getSolutionId(), gopayIntTxnCd,solutionType,acctProdCode);
			
			return ProcCodeConstants.PROC_CODE_000S1000;
		}

		Map<String, Object> attrs = new HashMap<String, Object>();
		attrs.put("intTxnCd", gopayIntTxnCd);

		// 如果请求参数存在有效数据，但关系表不存在对应记录，则新建关系记录，并保存新方案及明细信息
		if (rel == null) {
			// 2: save rel, save solution, save transDtl
			rel = createApSolRel(acctProdCode, solutionType);
			Solution s = new Solution();
			SolutionMeta meta = createSolutionMeta(solutionType);
			List<SolutionRuleDtl> dlts = createRuleDtls(dtlInfos, attrs,
					solutionType, meta.getSolutionId());
			s.setMeta(meta);
			s.setRuleDtls(dlts);
			ssApSolutionManager.saveRelAndChild(rel, s);
			return ProcCodeConstants.PROC_CODE_000S1000;
		}

		// 方案及明细均存在，先删除旧的明细信息，再保存新的明细信息
		Solution s = ssApSolutionManager.load(rel.getSolutionId(),
				gopayIntTxnCd, solutionType);
		if (s != null) {
			// 新的dtl赋值到solution中
			List<SolutionRuleDtl> dlts = createRuleDtls(dtlInfos, attrs,
					solutionType, s.getMeta().getSolutionId());
			s.setRuleDtls(dlts);
			ssApSolutionManager.updateSolutionChild(s, gopayIntTxnCd,solutionType,acctProdCode);
			return ProcCodeConstants.PROC_CODE_000S1000;
		}
		return "方案不存在，无法操作！产品代码：" + acctProdCode + "，交易码：" + gopayIntTxnCd;
	}

	/**
	 * 创建一个新的方案
	 * 
	 * @return
	 */
	private SolutionMeta createSolutionMeta(String solutionType) {
		SolutionMeta meta = null;
		if (SolutionTypeConstant.TRANS_TRN.equals(solutionType)) {
			meta = new SsTransSolution();
		} else if (SolutionTypeConstant.TRANS_FEE.equals(solutionType)) {
			meta = new SsTransFeeSolution();
		} else if (SolutionTypeConstant.TRANS_PAYCHAN.equals(solutionType)) {
			meta = new SsTransPayChanSolution();
		} else if (SolutionTypeConstant.ACCT_GAIN.equals(solutionType)) {
			meta = new SsAcctGainsSolution();
		} else if (SolutionTypeConstant.TRANS_UIS.equals(solutionType)) {
			meta = new SsUIConfigSolution();
		} else if (SolutionTypeConstant.CUST_TLCS.equals(solutionType)) {
			meta = new SsTransLimitConfSolution();
		}else if(SolutionTypeConstant.GP_COM.equals(solutionType)){
		    meta = new SsGpComSolution();
		}else if(SolutionTypeConstant.GP_RISKFUND.equals(solutionType)){
		    meta = new SsGpRiskFundConfSolution();
		} else if (SolutionTypeConstant.TRANS_PROFIT.equals(solutionType)) {
            meta = new SsTransProfitSolution();
        } else if (SolutionTypeConstant.TRANS_SETTLE.equals(solutionType)) {
            meta = new SsTransSettleSolution();
        }
		meta.setCreateTime(DateUtils.getSystemDate());
		meta.setLastUpdTime(meta.getCreateTime());
		meta.setSolutionCode(0);
		meta.setSolutionDesc("add solution");
		meta.setSolutionId(System.currentTimeMillis());
		meta.setSolutionName("solution");
		meta.setSolutionVersion(0);
		return meta;
	}

	/**
	 * 交易方案明细记录
	 * 
	 * @param solutionType
	 * @param solutionId
	 * @return
	 */
	private List<SolutionRuleDtl> createRuleDtls(List<SolutionDtlInfo> params,
			Map<String, Object> attrs, String solutionType, long solutionId) {
		AtomicInteger sq = new AtomicInteger(1);
		List<SolutionRuleDtl> dlts = new ArrayList<SolutionRuleDtl>();
		for (int i = 0, size = params == null ? 0 : params.size(); i < size; i++) {
			SolutionRuleDtl dtl = getSolutionRuleDtl(params.get(i), attrs,
					solutionType);
			dtl.setCreateTime(DateUtils.getSystemDate());
			dtl.setLastUpdTranCode("");
			dtl.setLastUpdTime(dtl.getCreateTime());
			dtl.setSolutionId(solutionId);
			dtl.setSq(System.currentTimeMillis() + sq.getAndIncrement());
			dlts.add(dtl);
		}
		return dlts;
	}

	/**
	 * 
	 * @param param
	 * @param attrs
	 * @param solutionType
	 * @return
	 */
	private SolutionRuleDtl getSolutionRuleDtl(SolutionDtlInfo param,
			Map<String, Object> attrs, String solutionType) {
		if (SolutionTypeConstant.TRANS_FEE.equals(solutionType)) {
			FeeDtlInfo info = (FeeDtlInfo) param;
			SsTransFeeDtl dtl = new SsTransFeeDtl();
			dtl.setAcctType(info.getAcctType());
			dtl.setCalExp(info.getCalExp());
			dtl.setGatewaySource("00");
			dtl.setFeeType(info.getFeeType());
			dtl.setGopayIntTxnCd((String) attrs.get("intTxnCd"));
			dtl.setPayChannel(info.getPayChannel());
			dtl.setRemarks(info.getRemarks());
			dtl.setDtlStatus(info.getDtlStatus());
			//跨境加上邮电费 qld
			dtl.setPostAmt(info.getPostAmt());
			dtl.setWkQuotaFeeAmt(info.getWkQuotaFeeAmt());
			dtl.setWkComplaintFeeAmt(info.getWkComplaintFeeAmt());
			return dtl;
		}
		if (SolutionTypeConstant.TRANS_PAYCHAN.equals(solutionType)) {
			PayChannelDtlInfo info = (PayChannelDtlInfo) param;
			SsTransPayChanDtl dtl = new SsTransPayChanDtl();
			dtl.setGopayIntTxnCd((String) attrs.get("intTxnCd"));
			dtl.setPayChannel(info.getPayChannel());
			dtl.setOutStlmId(info.getOutStlmId());
			return dtl;
		}
		if (SolutionTypeConstant.ACCT_GAIN.equals(solutionType)) {
			GainDtlInfo info = (GainDtlInfo) param;
			SsAcctGainDtl dtl = new SsAcctGainDtl();
			dtl.setAcctId(info.getAcctId());
			dtl.setBeginDate(info.getBeginDate());
			dtl.setCalExp(info.getCalExp());
			dtl.setEndDate(info.getEndDate());
			dtl.setGainsType(Integer.valueOf(info.getGainsType()));
			return dtl;
		}
		if (SolutionTypeConstant.TRANS_TRN.equals(solutionType)) {
			TransDtlInfo info = (TransDtlInfo) param;
			SsTransDtl dtl = new SsTransDtl();
			dtl.setGatewaySource("00");
			dtl.setGopayIntTxnCd(info.getGopayIntTxnCd());
			return dtl;
		}
		if (SolutionTypeConstant.TRANS_UIS.equals(solutionType)) {
			UIConfigDtlInfo info = (UIConfigDtlInfo) param;
			SsUIConfigDtl dtl = new SsUIConfigDtl();
			dtl.setAppID(info.getAppID());
			dtl.setBeginDate(info.getBeginDate());
			dtl.setConfigExp(info.getConfigExp());
			dtl.setEndDate(info.getEndDate());
			return dtl;
		} else if (SolutionTypeConstant.CUST_TLCS.equals(solutionType)) {

			SsTransLimitConfInfo info = (SsTransLimitConfInfo) param;
			SsTransLimitConfDtl dtl = new SsTransLimitConfDtl();

			dtl.setAcctType(info.getActType());
			dtl.setDayLimit(info.getDayLimit());
			dtl.setDayTimes(Long.parseLong(info.getDayTimes()));
			dtl.setGopayIntTxnCd(info.getGopayTxnCd());
			dtl.setIsEnabled(Integer.parseInt(info.getIsEnabled()));
			dtl.setOnceLimit(info.getOnceLimit());
			if (info.getSamePayeeLimmit() != null) {
				dtl.setSavePayeeLimit(new BigDecimal(info.getSamePayeeLimmit()
						.toString()));
			}

			if(StringUtils.isNotBlank(info.getSolutionId())){
				dtl.setSolutionId(Long.parseLong(info.getSolutionId()));
			}
			
			if (StringUtils.isNotBlank(info.getSq())) {
				dtl.setSq(Long.parseLong(info.getSq()));
			}
			dtl.setTranDirection(info.getTranDirection());
			return dtl;
		}else if(SolutionTypeConstant.GP_COM.equals(solutionType)) {
		    SsComConfInfo info = (SsComConfInfo) param;
            SsGpComConfDtl dtl = new SsGpComConfDtl();
            dtl.setAcctType(info.getAcctType());
            dtl.setCalExp(info.getCalExp());
            dtl.setGatewaySource("00");
            dtl.setFeeType(info.getFeeType());
            dtl.setGopayIntTxnCd("");
            dtl.setPayChannel(info.getPayChannel());
            dtl.setRemarks(info.getRemarks());
            dtl.setDtlStatus(info.getDtlStatus());
            return dtl;
        }else if(SolutionTypeConstant.GP_RISKFUND.equals(solutionType)){
            SsRskConfInfo info = (SsRskConfInfo) param;
            SsGpRiskFundConfDtl dtl = new SsGpRiskFundConfDtl();
            dtl.setAcctType(info.getAcctType());
            dtl.setCalExp(info.getCalExp());
            dtl.setGatewaySource("00");
            dtl.setFeeType(info.getFeeType());
            dtl.setGopayIntTxnCd("");
            dtl.setPayChannel(info.getPayChannel());
            dtl.setRemarks(info.getRemarks());
            dtl.setDtlStatus(info.getDtlStatus());
            return dtl;
        }else if(SolutionTypeConstant.TRANS_PROFIT.equals(solutionType)) {
            ProfitDtlInfo info = (ProfitDtlInfo) param;
            SsTransProfitDtl dtl = new SsTransProfitDtl();
            dtl.setGopayIntTxnCd((String) attrs.get("intTxnCd"));
            dtl.setGatewaySource("00");
            dtl.setTransAuth(info.getTransAuth());
            dtl.setTransStartDate(info.getTransStartDate());
            dtl.setTransEndDate(info.getTransEndDate());
            dtl.setIsEnable(info.getIsEnable());
            dtl.setAcctType(info.getAcctType());
            dtl.setCalExp(info.getCalExp());
            dtl.setPayChannel(info.getPayChannel());
            dtl.setRemarks(info.getRemarks());
            dtl.setDtlStatus(info.getDtlStatus());
            return dtl;
        }else if(SolutionTypeConstant.TRANS_SETTLE.equals(solutionType)) {
            SettleDtlInfo info = (SettleDtlInfo) param;
            SsTransSettleDtl dtl = new SsTransSettleDtl();
            dtl.setGopayIntTxnCd((String) attrs.get("intTxnCd"));
			dtl.setAcctId(info.getAcctId());
			dtl.setBankCode(info.getBankCode());
			/**自动结算优化  增加工作日和节假日标识  zhg.zhang 20160727**/
			dtl.setIsWorkDay(info.getIsWorkDay());
            return dtl;
        }
		return null;
	}

	/**
	 * 账户方案关系
	 * 
	 * @param acctProdCode
	 * @param solutionType
	 * @return
	 */
	private SsCapSolutionRel createCapSolRel(String acctProdCode,
			String solutionType, String custId) {
		SsCapSolutionRel rel = new SsCapSolutionRel();
		rel.setCreatedTs(DateUtils.getSystemDate());
		rel.setLastUpdTs(rel.getCreatedTs());
		rel.setProdCode(acctProdCode);
		rel.setSolutionType(solutionType);
		rel.setSq(System.currentTimeMillis());
		rel.setSolutionId(0);
		rel.setCustId(custId);
		return rel;
	}

	/**
	 * 客户方案关系
	 * 
	 * @param acctProdCode
	 * @param solutionType
	 * @return
	 */
	private SsApSolutionRel createApSolRel(String acctProdCode,
			String solutionType) {
		SsApSolutionRel rel = new SsApSolutionRel();
		rel.setCreatedTs(DateUtils.getSystemDate());
		rel.setLastUpdTs(rel.getCreatedTs());
		rel.setProdCode(acctProdCode);
		rel.setSolutionType(solutionType);
		rel.setSq(System.currentTimeMillis());
		rel.setSolutionId(0);
		return rel;
	}

	/**
	 * 检查账户产品
	 * 
	 * @param acctProdCode
	 * @return
	 */
	private String checkProdCode(String acctProdCode) {
		if (StringUtils.isBlank(acctProdCode)) {
			return "产品代码错误: " + acctProdCode;
		}

		// 校验产品代码
		CpsAcctProdMast mast = cpsAcctProdMastQueryDAO.find(
				CpsAcctProdMast.class, acctProdCode);
		if (mast == null) {
			return "平台不支持的产品代码: " + acctProdCode;
		}
		return null;
	}

	/**
	 * 
	 * @param solutionType
	 * @return
	 */
	private String checkIntTxnCds(String intTxnCd, String solutionType) {

		if (StringUtils.isEmpty(intTxnCd)) {
			if (SolutionTypeConstant.TRANS_TRN.equals(solutionType)
					|| SolutionTypeConstant.TRANS_UIS.equals(solutionType)) {
				return null;
			} else {
				return "交易码不能为空";
			}
		}

		if (null == cpsTransInfoQueryDAO.find(CpsTransInfo.class, intTxnCd)) {
			return "平台不支持的交易码：" + intTxnCd;
		}
		return null;
	}

	/**
	 * 
	 * @param rel
	 * @return
	 */
	private SsApSolution initSsApSolution(SsApSolutionRel rel) {

		if (null == rel) {
			return null;
		}

		SsApSolution aps = new SsApSolution();
		aps.setProdCode(rel.getProdCode());
		aps.setProdName("");
		aps.setSsApSolutionRelSq("" + rel.getSq());
		aps.setSolutionType(rel.getSolutionType());
		aps.setSolutionId("" + rel.getSolutionId());

		return aps;
	}

	/**
	 * 
	 * @param aps
	 * @param solutionType
	 * @param gopayIntTxnCd
	 */
	private void setSsApSolutionDtlInfos(SsApSolution aps, String solutionType,
			String gopayIntTxnCd) {
		if (aps == null) {
			return;
		}
		Solution s = ssApSolutionManager.load(
				Long.valueOf(aps.getSolutionId()), gopayIntTxnCd, solutionType);
		aps.setSolutionDtls(initSolutionDtlInfos(s, solutionType));
	}

	/**
	 * 
	 * @param aps
	 * @return
	 */
	private void setSsApSolutionDtlInfos(SsApSolution aps, String solutionType) {
		if (aps == null) {
			return;
		}
		Solution s = ssApSolutionManager.load(
				Long.valueOf(aps.getSolutionId()), solutionType);
		if (s != null) {
			aps.setEndDate(s.getMeta().getEndDate());
			aps.setStartDate(s.getMeta().getStartDate());
			aps.setSolutionDtls(initSolutionDtlInfos(s, solutionType));
		}

	}

	/**
	 * 
	 * @param solution
	 * @param solutionType
	 * @return
	 */
	private List<SolutionDtlInfo> initSolutionDtlInfos(Solution solution,
			String solutionType) {
		if (null == solution) {
			return null;
		}
		List<SolutionRuleDtl> ruleDtls = solution.getRuleDtls();
		if (null == ruleDtls || ruleDtls.isEmpty()) {
			return null;
		}
		List<SolutionDtlInfo> list = new ArrayList<SolutionDtlInfo>();
		for (int i = 0, size = ruleDtls.size(); i < size; i++) {
			list.add(initSolutionDtlInfo(ruleDtls.get(i), solutionType));
		}
		return list;
	}

	private SolutionDtlInfo initSolutionDtlInfo(SolutionRuleDtl ruleDtl,
			String solutionType) {
		if (SolutionTypeConstant.TRANS_FEE.equals(solutionType)) {
			SsTransFeeDtl dtl = (SsTransFeeDtl) ruleDtl;
			FeeDtlInfo info = new FeeDtlInfo();
			info.setCalExp(dtl.getCalExp());
			info.setFeeType(dtl.getFeeType());
			info.setPayChannel(dtl.getPayChannel());
			info.setAcctType(dtl.getAcctType());
			info.setRemarks(dtl.getRemarks());
			info.setDtlStatus(dtl.getDtlStatus());
			info.setSeq(dtl.getSq());
			info.setIntTxnCd(dtl.getGopayIntTxnCd());
			//跨境的邮电费
			info.setPostAmt(dtl.getPostAmt());
			info.setWkQuotaFeeAmt(dtl.getWkQuotaFeeAmt());
			info.setWkComplaintFeeAmt(dtl.getWkComplaintFeeAmt());
			return info;
		} else if (SolutionTypeConstant.TRANS_PAYCHAN.equals(solutionType)) {
			SsTransPayChanDtl dtl = (SsTransPayChanDtl) ruleDtl;
			PayChannelDtlInfo info = new PayChannelDtlInfo();
			info.setOutStlmId(dtl.getOutStlmId());
			info.setPayChannel(dtl.getPayChannel());
			return info;
		}
		if (SolutionTypeConstant.ACCT_GAIN.equals(solutionType)) {
			SsAcctGainDtl dtl = (SsAcctGainDtl) ruleDtl;
			GainDtlInfo info = new GainDtlInfo();
			info.setAcctId(dtl.getAcctId());
			info.setBeginDate(dtl.getBeginDate());
			info.setCalExp(dtl.getCalExp());
			info.setEndDate(dtl.getEndDate());
			info.setGainsType("" + dtl.getGainsType());
			info.setCalTime(dtl.getCalTime());
			return info;
		} else if (SolutionTypeConstant.TRANS_TRN.equals(solutionType)) {
			SsTransDtl dtl = (SsTransDtl) ruleDtl;
			TransDtlInfo info = new TransDtlInfo();
			info.setGopayIntTxnCd(dtl.getGopayIntTxnCd());
			info.setGopayIntTxnName("");
			return info;
		} else if (SolutionTypeConstant.TRANS_UIS.equals(solutionType)) {
			SsUIConfigDtl dtl = (SsUIConfigDtl) ruleDtl;
			UIConfigDtlInfo info = new UIConfigDtlInfo();
			info.setAppID(dtl.getAppID());
			info.setBeginDate(dtl.getBeginDate());
			info.setEndDate(dtl.getEndDate());
			info.setConfigExp(dtl.getConfigExp());
			return info;
		} else if (SolutionTypeConstant.CUST_TLCS.equals(solutionType)) {
			SsTransLimitConfDtl dtl = (SsTransLimitConfDtl) ruleDtl;
			SsTransLimitConfInfo info = new SsTransLimitConfInfo();
			info.setActType(dtl.getAcctType());
			info.setDayLimit(dtl.getDayLimit());
			info.setDayTimes(dtl.getDayTimes() + "");
			info.setGopayTxnCd(dtl.getGopayIntTxnCd());
			info.setIsEnabled(dtl.getIsEnabled() + "");
			info.setOnceLimit(dtl.getOnceLimit());
			info.setSamePayeeLimmit(dtl.getSavePayeeLimit());
			info.setSolutionId(String.valueOf(dtl.getSolutionId()));
			info.setSq(String.valueOf(dtl.getSq()));
			info.setTranDirection(dtl.getTranDirection());
			return info;
		}else if(SolutionTypeConstant.GP_COM.equals(solutionType)){
		    SsGpComConfDtl dtl = (SsGpComConfDtl) ruleDtl;
		    SsComConfInfo info = new SsComConfInfo();
            info.setCalExp(dtl.getCalExp());
            info.setFeeType(dtl.getFeeType());
            info.setPayChannel(dtl.getPayChannel());
            info.setAcctType(dtl.getAcctType());
            info.setRemarks(dtl.getRemarks());
            info.setDtlStatus(dtl.getDtlStatus());
            info.setSeq(dtl.getSq());
            return info;
		}else if(SolutionTypeConstant.GP_RISKFUND.equals(solutionType)){
		    SsGpRiskFundConfDtl dtl = (SsGpRiskFundConfDtl) ruleDtl;
		    SsRskConfInfo info = new SsRskConfInfo();
            info.setCalExp(dtl.getCalExp());
            info.setFeeType(dtl.getFeeType());
            info.setPayChannel(dtl.getPayChannel());
            info.setAcctType(dtl.getAcctType());
            info.setRemarks(dtl.getRemarks());
            info.setDtlStatus(dtl.getDtlStatus());
            info.setSeq(dtl.getSq());
            return info;
        } else if (SolutionTypeConstant.TRANS_PROFIT.equals(solutionType)) {
            SsTransProfitDtl dtl = (SsTransProfitDtl) ruleDtl;
            ProfitDtlInfo info = new ProfitDtlInfo();
            info.setTransAuth(dtl.getTransAuth());
            info.setTransStartDate(dtl.getTransStartDate());
            info.setTransEndDate(dtl.getTransEndDate());
            info.setIsEnable(dtl.getIsEnable());
            info.setCalExp(dtl.getCalExp());
            info.setPayChannel(dtl.getPayChannel());
            info.setAcctType(dtl.getAcctType());
            info.setRemarks(dtl.getRemarks());
            info.setDtlStatus(dtl.getDtlStatus());
            info.setSeq(dtl.getSq());            
            return info;
        } else if (SolutionTypeConstant.TRANS_SETTLE.equals(solutionType)) {
        	SsTransSettleDtl dtl = (SsTransSettleDtl) ruleDtl;
        	SettleDtlInfo info = new SettleDtlInfo();
        	info.setAcctId(dtl.getAcctId());
        	info.setBankCode(dtl.getBankCode());
        	/**自动结算优化  增加工作日和节假日标识  zhg.zhang 20160727**/
        	info.setIsWorkDay(dtl.getIsWorkDay());
            info.setSeq(dtl.getSq());            
            return info;
        }
		return null;
	}

	@Override
	public List<SsPlTranSolution> findPlTransSolutionByPlId(
			List<String> prodLineIds) {

		if (prodLineIds == null || prodLineIds.size() < 1) {
			return null;
		}

		List<SsPlSolutionRel> rels = ssPlSolutionRelDAO.listByPlIdSoluType(
				prodLineIds, SolutionTypeConstant.TRANS_PRODLINE_TRANS);

		if (rels != null && rels.size() > 0) {

			List<SsPlTranSolution> ret = new ArrayList<SsPlTranSolution>();

			for (SsPlSolutionRel rel : rels) {

				Solution solu = ssProdLineTransSoluManager.load(rel
						.getSolutionId());

				SsPlTranSolution ss = new SsPlTranSolution();
				ss.setProdLineId(rel.getProdLineId());

				SolutionMeta meta = solu.getMeta();
				ss.setSolutionId(meta.getSolutionId());
				ss.setSolutionCode(meta.getSolutionCode());
				ss.setSolutionVersion(meta.getSolutionVersion());
				ss.setSolutionName(meta.getSolutionName());
				ss.setSolutionDesc(meta.getSolutionDesc());

				List<SolutionRuleDtl> dtls = solu.getRuleDtls();
				if (dtls != null && dtls.size() > 0) {
					List<String> trans = new ArrayList<String>();
					for (SolutionRuleDtl dtl : dtls) {
						trans.add(((SsProdLineTransDtl) dtl).getGopayIntTxnCd());
					}

					ss.setGopayIntTxnCds(trans);
				}

				ret.add(ss);
			}

			return ret;
		}
		return null;
	}

	@Override
	public String savePlTransSolution(SsPlTranSolution ssPlTransSolution) {
		try {
			String prodLineName = ssPlTransSolution.getProdLineName();
			List<String> prodCodes = ssPlTransSolution.getAcctProdCodes();
			List<String> trans = ssPlTransSolution.getGopayIntTxnCds();
			ssPlSolutionManager.saveSsPlTranSolution(prodLineName, prodCodes,
					trans);
		} catch (Exception e) {

			// 错误码
			if (e instanceof GopayCheckException) {
				return ((GopayCheckException) e).getErrCode();
			}

			// 失败
			return "";
		}

		// 成功
		return "";
	}

	@Override
	public String editPlTransSolution(SsPlTranSolution ssPlTransSolution) {
		try {
			String prodLineId = ssPlTransSolution.getProdLineId();
			String prodLineName = ssPlTransSolution.getProdLineName();
			List<String> prodCodes = ssPlTransSolution.getAcctProdCodes();
			List<String> trans = ssPlTransSolution.getGopayIntTxnCds();
			ssPlSolutionManager.editSsPlTranSolution(prodLineId, prodLineName,
					prodCodes, trans);
		} catch (Exception e) {

			// 错误码
			if (e instanceof GopayCheckException) {
				return ((GopayCheckException) e).getErrCode();
			}

			// 失败
			return "";
		}

		// 成功
		return "";
	}

	@Override
	public SsTranBasicSolution findTbSoluByApCodeAndIntTxnCd(
			String acctProdCode, String gopayIntTxnCd) {
		if (StringUtils.isNotEmpty(acctProdCode)
				&& StringUtils.isNotEmpty(gopayIntTxnCd)) {
			// 查找账户产品的交易方案关系表
			SsApSolutionRel rel = ssApSolutionRelDAO.getByApIdAndSoluType(
					acctProdCode, SolutionTypeConstant.TRANS_BASIC);
			if (rel != null) {
				// 根据交易方案ID和交易码获取不完整的交易方案（不完整的意思是指
				// 该方法得到的方案的明细仅仅和传入的交易码相关，不相关方案明细未被加载到该方案实例中）
				Solution solu = ssTransBasicSoluManager.loadBySeqAndIntTxnCd(
						rel.getSolutionId(), gopayIntTxnCd);
				if (solu != null && solu.getMeta() != null) {
					SsTranBasicSolution soluVo = new SsTranBasicSolution();

					soluVo.setSolutionId(solu.getMeta().getSolutionId());
					soluVo.setSolutionCode(solu.getMeta().getSolutionCode());
					soluVo.setSolutionVersion(solu.getMeta()
							.getSolutionVersion());
					soluVo.setSolutionName(solu.getMeta().getSolutionName());
					soluVo.setSolutionDesc(solu.getMeta().getSolutionDesc());

					List<TranBasicDtlInfo> dtls = new ArrayList<TranBasicDtlInfo>();
					List<SolutionRuleDtl> rules = solu.getRuleDtls();
					if (rules != null) {
						for (SolutionRuleDtl rule : rules) {
							SsTransBasicDtl dtlRule = (SsTransBasicDtl) rule;

							TranBasicDtlInfo dtl = new TranBasicDtlInfo();
							dtl.setGopayIntTxnCd(dtlRule.getGopayIntTxnCd());
							dtl.setIsEnable(dtlRule.getIsEnable());
							dtl.setFeePayer(dtlRule.getFeePayer());
							dtl.setSettleCirCle(dtlRule.getSettleCirCle());
							dtl.setIsRefundFee(dtlRule.getIsRefundFee());
							dtl.setIsOptimizingSettle(dtlRule.getIsOptimizingSettle());
							dtl.setIsMobileNotify(dtlRule.getIsMobileNotify());//短信通知
							if (StringUtils.isNotEmpty(dtlRule
									.getTranSpCustType())) {
								 dtl.setTranSpCustType(TranSpCustType.getTranSpCustTypeByValue(dtlRule.getTranSpCustType()));
							}
	                        dtl.setRefundPeriod(dtlRule.getRefundPeriod());
	                        //add by qld 最低结算限额
	                        dtl.setMinLimitAmt(dtlRule.getMinLimitAmt());
                            dtl.setCbrmbImportSplit(dtlRule.getCbrmbImportSplit());
                            dtl.setWkDepositScale(dtlRule.getWkDepositScale());
							dtl.setIsOverTimeRefund(dtlRule.getIsOverTimeRefund());
							dtl.setEndRefundPeriod(dtlRule.getEndRefundPeriod());

							// add by zhaoxubing 20170328 begin
							if(IntTxnCd._00701.value.equals(gopayIntTxnCd)){ // 如果是退款转账交易，取收款退款的退款期限
								Solution solu01203 = ssTransBasicSoluManager.loadBySeqAndIntTxnCd(
										rel.getSolutionId(), IntTxnCd._01203.value);

								if(null == solu01203 || CollectionUtils.isEmpty(solu01203.getRuleDtls())){
									dtl.setEndRefundPeriodBegin(0);
								}else{
									dtl.setEndRefundPeriodBegin(((SsTransBasicDtl)solu01203.getRuleDtls().get(0)).getRefundPeriod());
								}
							}
							// add by zhaoxubing 20170328 end
							//保证金 20170413  add by jianglijun start
							if(IntTxnCd._00501.value.equals(gopayIntTxnCd)) {
								dtl.setDepositAmt(dtlRule.getDepositAmt());
							}
							//保证金 20170413  add by jianglijun end
							// D1优化增加结算方式, 结算周期 add by zhengxue20170719
							dtl.setSettlementType(dtlRule.getSettlementType());
							dtl.setSettlementOperationType(dtlRule.getSettlementOperationType());
			                
							dtls.add(dtl);
						}
					}
					soluVo.setDtls(dtls);

					soluVo.setAcctProdCode(acctProdCode);
					soluVo.setGopayIntTxnCd(gopayIntTxnCd);

					return soluVo;
				}
			}
		}

		return null;
	}

	@Override
	public String editTbSoluByIntTxnCd(SsTranBasicSolution ssTranBasicSolution,
			String gopayIntTxnCd) {
		try {

			if (ssTranBasicSolution == null
					|| StringUtils.isEmpty(gopayIntTxnCd)) {
				return "";
			}

			List<TranBasicDtlInfo> dtls = ssTranBasicSolution.getDtls();
			if (dtls == null || dtls.isEmpty()) {
				return "";
			}

			// 根据交易方案ID和交易码获取不完整的交易方案（不完整的意思是指
			// 该方法得到的方案的明细仅仅和传入的交易码相关，不相关方案明细未被加载到该方案实例中）
			Solution solu = ssTransBasicSoluManager.loadBySeqAndIntTxnCd(
					ssTranBasicSolution.getSolutionId(), gopayIntTxnCd);
			if (solu == null) {
				return "";
			}

			List<SolutionRuleDtl> rules = new ArrayList<SolutionRuleDtl>();
			int offset = 0;
			long startSq = System.currentTimeMillis();
			for (TranBasicDtlInfo dtl : dtls) {
				SsTransBasicDtl rule = new SsTransBasicDtl();
				rule.setSq(startSq + ++offset);
				rule.setSolutionId(ssTranBasicSolution.getSolutionId());
				rule.setGopayIntTxnCd(dtl.getGopayIntTxnCd());
				rule.setIsEnable(dtl.getIsEnable());
				rule.setFeePayer(dtl.getFeePayer());
				rule.setSettleCirCle(dtl.getSettleCirCle());
				rule.setIsRefundFee(dtl.getIsRefundFee());
				if(dtl.getTranSpCustType() != null) {
					rule.setTranSpCustType(dtl.getTranSpCustType().value);
				}
                rule.setRefundPeriod(dtl.getRefundPeriod());

				rule.setCreateTime(DateUtils.getSystemDate());
				rule.setLastUpdTime(DateUtils.getSystemDate());
				rule.setLastUpdTranCode("");
				rule.setIsOptimizingSettle(dtl.getIsOptimizingSettle());
                //加上最低限额
				rule.setMinLimitAmt(dtl.getMinLimitAmt());
				rule.setCbrmbImportSplit(dtl.getCbrmbImportSplit());
				rule.setWkDepositScale(dtl.getWkDepositScale());
				rule.setIsMobileNotify(dtl.getIsMobileNotify());//短信通知
				rule.setIsOverTimeRefund(dtl.getIsOverTimeRefund());
				rule.setEndRefundPeriod(dtl.getEndRefundPeriod());
				//保证金 20170413  add by jianglijun start
				if(IntTxnCd._00501.value.equals(dtl.getGopayIntTxnCd())) {
					rule.setDepositAmt(dtl.getDepositAmt());//保证金金额
				}
				//保证金 20170413  add by jianglijun end
				// D1优化增加结算方式, 结算周期 add by zhengxue20170719
				rule.setSettlementType(dtl.getSettlementType());
				rule.setSettlementOperationType(dtl.getSettlementOperationType());

				rules.add(rule);
			}

			ssTbSolutionManager.editSsTbSolution(ssTranBasicSolution.getSolutionId(), gopayIntTxnCd, rules,ssTranBasicSolution.getAcctProdCode());

			// succeed
			return "";
		} catch (Exception e) {
			// 错误码
			if (e instanceof GopayCheckException) {
				return ((GopayCheckException) e).getErrCode();
			}

			// 失败
			return "";
		}
	}

	@Override
	public String saveTbSoluByIntTxnCd(SsTranBasicSolution ssTranBasicSolution,
			String acctProdCode, String gopayIntTxnCd) {
		try {
			if (ssTranBasicSolution == null
					|| StringUtils.isEmpty(acctProdCode)
					|| StringUtils.isEmpty(gopayIntTxnCd)) {
				return "";
			}

			List<TranBasicDtlInfo> dtls = ssTranBasicSolution.getDtls();
			if (dtls == null || dtls.isEmpty()) {
				return "";
			}

			// 获取交易方案
			Solution solu = ssTransBasicSoluManager.load(ssTranBasicSolution
					.getSolutionId());
			// 如果方案不存在，则新建方案并建立与该方案与账户产品的关系,如果存在，则表示该方案元数据已创建，和该交易相关的方案明细信息还未初始化
			if (solu == null) {
				solu = new Solution();
				SolutionMeta meta = new SsTransBasicSolution();
				meta.setSolutionId(ssTransBasicSoluManager.nextSolutionId());
				meta.setSolutionCode(1L);
				meta.setSolutionVersion(1L);
				meta.setSolutionName(meta.getSolutionId() + "");
				meta.setSolutionDesc(meta.getSolutionId() + "");
				meta.setCreateTime(DateUtils.getSystemDate());
				meta.setLastUpdTime(DateUtils.getSystemDate());
				meta.setLastUpdTranCode("");

				solu.setMeta(meta);
			}

			List<SolutionRuleDtl> rules = new ArrayList<SolutionRuleDtl>();
			int offset = 0;
			long startSq = System.currentTimeMillis();
			for (TranBasicDtlInfo dtl : dtls) {
				SsTransBasicDtl rule = new SsTransBasicDtl();
				rule.setSq(startSq + ++offset);
				rule.setSolutionId(solu.getMeta().getSolutionId());
				rule.setGopayIntTxnCd(dtl.getGopayIntTxnCd());
				rule.setIsEnable(dtl.getIsEnable());
				rule.setFeePayer(dtl.getFeePayer());
				rule.setSettleCirCle(dtl.getSettleCirCle());
				rule.setIsRefundFee(dtl.getIsRefundFee());
				if(dtl.getTranSpCustType() != null) {
					rule.setTranSpCustType(dtl.getTranSpCustType().value);
				}
				rule.setRefundPeriod(dtl.getRefundPeriod());

				rule.setCreateTime(DateUtils.getSystemDate());
				rule.setLastUpdTime(DateUtils.getSystemDate());
				rule.setLastUpdTranCode("");
				rule.setIsOptimizingSettle(dtl.getIsOptimizingSettle());
				rule.setIsMobileNotify(dtl.getIsMobileNotify());//短信通知
				rule.setIsOverTimeRefund(dtl.getIsOverTimeRefund());
				rule.setEndRefundPeriod(dtl.getEndRefundPeriod());

				// D1优化增加结算方式, 结算周期 add by zhengxue20170719
                rule.setSettlementType(dtl.getSettlementType());
                rule.setSettlementOperationType(dtl.getSettlementOperationType());
				rule.setCbrmbImportSplit(dtl.getCbrmbImportSplit());
				rule.setWkDepositScale(dtl.getWkDepositScale());
				rules.add(rule);
			}
			solu.setRuleDtls(rules);

			ssTbSolutionManager.saveSsTbSolution(solu, gopayIntTxnCd,
					OprLevel.PROD, acctProdCode,ssTranBasicSolution.getAcctProdCode());

			// succeed
			return "";
		} catch (Exception e) {
			// 错误码
			if (e instanceof GopayCheckException) {
				return ((GopayCheckException) e).getErrCode();
			}

			// 失败
			return "";
		}
	}

	@Override
	public SsTranLegalObjSolution findTloSoluByApCodeAndIntTxnCd(String apCode,
			String gopayIntTxnCd) {
		try {
			if (StringUtils.isNotEmpty(apCode)
					&& StringUtils.isNotEmpty(gopayIntTxnCd)) {
				// 查找账户产品的交易方案关系表
				SsApSolutionRel rel = ssApSolutionRelDAO.getByApIdAndSoluType(
						apCode, SolutionTypeConstant.TRANS_LEGALOBJ);
				if (rel != null) {
					// 根据交易方案ID和交易码获取不完整的交易方案（不完整的意思是指
					// 该方法得到的方案的明细仅仅和传入的交易码相关，不相关方案明细未被加载到该方案实例中）
					Solution solu = ssTransLegalObjSoluManager
							.loadBySeqAndIntTxnCd(rel.getSolutionId(),
									gopayIntTxnCd);
					if (solu != null && solu.getMeta() != null) {
						SsTranLegalObjSolution soluVo = new SsTranLegalObjSolution();

						soluVo.setSolutionId(solu.getMeta().getSolutionId());
						soluVo.setSolutionCode(solu.getMeta().getSolutionCode());
						soluVo.setSolutionVersion(solu.getMeta()
								.getSolutionVersion());
						soluVo.setSolutionName(solu.getMeta().getSolutionName());
						soluVo.setSolutionDesc(solu.getMeta().getSolutionDesc());

						List<String> legalObjs = new ArrayList<String>();
						List<SolutionRuleDtl> rules = solu.getRuleDtls();
						if (rules != null) {
							for (SolutionRuleDtl rule : rules) {
								SsTransLegalObjDtl dtlRule = (SsTransLegalObjDtl) rule;
								legalObjs.add(dtlRule.getObjAcctProdCode());
							}
						}
						soluVo.setLegalObjs(legalObjs);

						soluVo.setAcctProdCode(apCode);
						soluVo.setGopayIntTxnCd(gopayIntTxnCd);

						return soluVo;
					}
				}
			}
		} catch (Exception e) {

		}
		return null;
	}

	@Override
	public String editLoSoluByIntTxnCd(SsTranLegalObjSolution solution,
			String gopayIntTxnCd) {
		try {
			if (solution == null || StringUtils.isEmpty(gopayIntTxnCd)) {
				return "";
			}

			List<String> legalObjs = solution.getLegalObjs();
			if (legalObjs == null || legalObjs.isEmpty()) {
				return "";
			}

			// 根据交易方案ID和交易码获取不完整的交易方案（不完整的意思是指
			// 该方法得到的方案的明细仅仅和传入的交易码相关，不相关方案明细未被加载到该方案实例中）
			Solution solu = ssTransLegalObjSoluManager.loadBySeqAndIntTxnCd(
					solution.getSolutionId(), gopayIntTxnCd);
			if (solu == null) {
				return "";
			}

			List<SolutionRuleDtl> rules = new ArrayList<SolutionRuleDtl>();
			int offset = 0;
			long startSq = System.currentTimeMillis();
			for (String legalObj : legalObjs) {
				SsTransLegalObjDtl rule = new SsTransLegalObjDtl();
				rule.setSq(startSq + ++offset);
				rule.setSolutionId(solution.getSolutionId());
				rule.setGopayIntTxnCd(gopayIntTxnCd);
				rule.setObjAcctProdCode(legalObj);
				rule.setCreateTime(DateUtils.getSystemDate());
				rule.setLastUpdTime(DateUtils.getSystemDate());
				rule.setLastUpdTranCode("");

				rules.add(rule);
			}

			ssLoSolutionManager.editSsTbSolution(solution.getSolutionId(),
					gopayIntTxnCd, rules);

			// succeed
			return "";
		} catch (Exception e) {
			// 错误码
			if (e instanceof GopayCheckException) {
				return ((GopayCheckException) e).getErrCode();
			}

			// 失败
			return "";
		}
	}

	@Override
	public String saveLoSoluByIntTxnCd(SsTranLegalObjSolution solution,
			String acctProdCode, String gopayIntTxnCd) {
		try {
			if (solution == null || StringUtils.isEmpty(acctProdCode)
					|| StringUtils.isEmpty(gopayIntTxnCd)) {
				return "";
			}

			List<String> legalObjs = solution.getLegalObjs();
			if (legalObjs == null || legalObjs.isEmpty()) {
				return "";
			}

			// 根据交易方案ID
			Solution solu = ssTransLegalObjSoluManager.load(solution
					.getSolutionId());
			// 如果方案不存在，则新建方案并建立与该方案与账户产品的关系,如果存在，则表示该方案元数据已创建，和该交易相关的方案明细信息还未初始化
			if (solu == null) {
				solu = new Solution();
				SolutionMeta meta = new SsTransLegalObjSolution();
				meta.setSolutionId(ssTransLegalObjSoluManager.nextSolutionId());
				meta.setSolutionCode(1L);
				meta.setSolutionVersion(1L);
				meta.setSolutionName(meta.getSolutionId() + "");
				meta.setSolutionDesc(meta.getSolutionId() + "");
				meta.setCreateTime(DateUtils.getSystemDate());
				meta.setLastUpdTime(DateUtils.getSystemDate());
				meta.setLastUpdTranCode("");

				solu.setMeta(meta);
			}

			List<SolutionRuleDtl> rules = new ArrayList<SolutionRuleDtl>();
			int offset = 0;
			long startSq = System.currentTimeMillis();
			for (String legalObj : legalObjs) {
				SsTransLegalObjDtl rule = new SsTransLegalObjDtl();
				rule.setSq(startSq + ++offset);
				rule.setSolutionId(solu.getMeta().getSolutionId());
				rule.setGopayIntTxnCd(gopayIntTxnCd);
				rule.setObjAcctProdCode(legalObj);

				rule.setCreateTime(DateUtils.getSystemDate());
				rule.setLastUpdTime(DateUtils.getSystemDate());
				rule.setLastUpdTranCode("");

				rules.add(rule);
			}
			solu.setRuleDtls(rules);

			ssLoSolutionManager.saveSsTbSolution(solu, acctProdCode,
					gopayIntTxnCd);

			// succeed
			return "";
		} catch (Exception e) {
			// 错误码
			if (e instanceof GopayCheckException) {
				return ((GopayCheckException) e).getErrCode();
			}

			// 失败
			return "";
		}
	}

	@Override
	public boolean isBankChannelUsed(String bankCode, String channel) {
		if (StringUtils.isBlank(bankCode) || StringUtils.isBlank(channel)) {
			return false;
		}
		return ssApSolutionManager.isBankChannelUsed(bankCode, channel);
	}

	@Override
	public SsTranBasicSolution findTranBasicSoluByProdCode(String acctProdCode) {
		// 获得所有账户产品的交易(TRN)方案
		SsApSolutionRel rel = ssApSolutionRelDAO.getByApIdAndSoluType(
				acctProdCode, SolutionTypeConstant.TRANS_BASIC);
		if (rel == null) {
			return null;
		}
		if (rel != null) {
			// 根据交易方案ID和交易码获取不完整的交易方案（不完整的意思是指
			// 该方法得到的方案的明细仅仅和传入的交易码相关，不相关方案明细未被加载到该方案实例中）
			Solution solu = ssTransBasicSoluManager.load(rel.getSolutionId());
			if (solu != null && solu.getMeta() != null) {
				SsTranBasicSolution soluVo = new SsTranBasicSolution();

				soluVo.setSolutionId(solu.getMeta().getSolutionId());
				soluVo.setSolutionCode(solu.getMeta().getSolutionCode());
				soluVo.setSolutionVersion(solu.getMeta().getSolutionVersion());
				soluVo.setSolutionName(solu.getMeta().getSolutionName());
				soluVo.setSolutionDesc(solu.getMeta().getSolutionDesc());

				List<TranBasicDtlInfo> dtls = new ArrayList<TranBasicDtlInfo>();
				List<SolutionRuleDtl> rules = solu.getRuleDtls();
				if (rules != null) {
					for (SolutionRuleDtl rule : rules) {
						SsTransBasicDtl dtlRule = (SsTransBasicDtl) rule;

						TranBasicDtlInfo dtl = new TranBasicDtlInfo();
						dtl.setGopayIntTxnCd(dtlRule.getGopayIntTxnCd());
						dtl.setIsEnable(dtlRule.getIsEnable());
						dtl.setFeePayer(dtlRule.getFeePayer());
						dtl.setSettleCirCle(dtlRule.getSettleCirCle());
						dtl.setIsRefundFee(dtlRule.getIsRefundFee());
						if (StringUtils.isNotEmpty(dtlRule.getTranSpCustType())) {
							dtl.setTranSpCustType(TranSpCustType.getTranSpCustTypeByValue(dtlRule.getTranSpCustType()));
						}
                        dtl.setRefundPeriod(dtlRule.getRefundPeriod());
						dtls.add(dtl);
					}
				}
				soluVo.setDtls(dtls);

				soluVo.setAcctProdCode(acctProdCode);

				return soluVo;
			} else {
				return null;
			}
		} else {
			return null;
		}

	}

	@Override
	public SsTransLimitConfInfo findSolutionDtl(Long sq) {
		SsTransLimitConfDtl ssTransLimitConfDtl = (SsTransLimitConfDtl) ssTransLimitConfDtlDAO
				.getByPk(sq);
		SsTransLimitConfInfo info = (SsTransLimitConfInfo) initSolutionDtlInfo(
				ssTransLimitConfDtl, SolutionTypeConstant.CUST_TLCS);
		return info;
	}

	@Override
	@Transactional
	public SsTransLimitConfInfo modifySolutionDtl(SsTransLimitConfInfo info) {
		// sq为null时添加明细
		if (info.getSq() == null || info.getSq().equals("")) {
			SsTransLimitConfDtl ssTransLimitConfDtl = initTransLimitDtl();
			ssTransLimitConfDtl.setDayLimit(info.getDayLimit());
			ssTransLimitConfDtl.setDayTimes(Long.parseLong(info.getDayTimes()));
			ssTransLimitConfDtl.setIsEnabled(Integer.parseInt(info
					.getIsEnabled()));
			ssTransLimitConfDtl.setOnceLimit(info.getOnceLimit());
			ssTransLimitConfDtl.setSavePayeeLimit(info.getSamePayeeLimmit());
			ssTransLimitConfDtl.setGopayIntTxnCd(info.getGopayTxnCd());
			ssTransLimitConfDtl.setTranDirection(info.getTranDirection());
			ssTransLimitConfDtl.setSolutionId(Long.parseLong(info
					.getSolutionId()));
			ssTransLimitConfDtl.setAcctType(info.getActType());
			info.setSq(ssTransLimitConfDtl.getSq() + "");
			ssTransLimitConfDtlDAO.save(ssTransLimitConfDtl);
			return info;
		} else {
			SsTransLimitConfDtl ssTransLimitConfDtl = (SsTransLimitConfDtl) ssTransLimitConfDtlDAO
					.getByPk(Long.valueOf((info.getSq())));
			ssTransLimitConfDtl.setDayLimit(info.getDayLimit());
			ssTransLimitConfDtl.setDayTimes(Long.parseLong(info.getDayTimes()));
			ssTransLimitConfDtl.setIsEnabled(Integer.parseInt(info
					.getIsEnabled()));
			ssTransLimitConfDtl.setOnceLimit(info.getOnceLimit());
			ssTransLimitConfDtl.setSavePayeeLimit(info.getSamePayeeLimmit());
			ssTransLimitConfDtl.setTranDirection(info.getTranDirection());
			ssTransLimitConfDtlDAO.save(ssTransLimitConfDtl);
			return info;
		}
	}

	private SsTransLimitConfDtl initTransLimitDtl() {
		SsTransLimitConfDtl ssTransLimitConfDtl = new SsTransLimitConfDtl();
		AtomicInteger sq = new AtomicInteger(1);
		ssTransLimitConfDtl.setSq((long) (System.currentTimeMillis() + sq
				.getAndIncrement()));
		ssTransLimitConfDtl.setCreateTime(DateUtils.getSystemDate());
		ssTransLimitConfDtl.setLastUpdTime(ssTransLimitConfDtl.getCreateTime());
		ssTransLimitConfDtl.setLastUpdTranCode("");
		return ssTransLimitConfDtl;
	}

    @Override
    public String modifyGpComConfSolution(SsApSolution ssApSolution,
            String solutionType) {
        //To determine whether a solution is empty
        if (ssApSolution == null) {
            return "账户产品方案设置：数据为空";
        }

        String acctProdCode = ssApSolution.getProdCode();

        String error = checkProdCode(acctProdCode);
        if (null != error) {
            return error;
        }

        List<SolutionDtlInfo> comDtlInfos = ssApSolution.getSolutionDtls();

        // 查询账户产品关系记录
        SsApSolutionRel rel = ssApSolutionManager.findRelByProdCode(
                acctProdCode, solutionType);

        // 如果请求参数为空，则表示删除此关系记录、方案记录及明细
        if (comDtlInfos == null || comDtlInfos.isEmpty()) {
            // 1: delete rel, delete solution, delete transDtl
            ssApSolutionManager.deleteRelAndChild(rel, solutionType);
            return ProcCodeConstants.PROC_CODE_200S1000;
        }

        // 如果请求参数存在有效数据，但关系表不存在对应记录，则新建关系记录，并保存新方案及明细信息
        if (rel == null) {
            // 2: save rel, save solution, save transDtl
            rel = createApSolRel(acctProdCode, solutionType);
            Solution s = new Solution();
            SolutionMeta meta = createSolutionMeta(solutionType);
            List<SolutionRuleDtl> dlts = createRuleDtls(comDtlInfos, null,
                    solutionType, meta.getSolutionId());
            s.setMeta(meta);
            s.setRuleDtls(dlts);
            ssApSolutionManager.saveRelAndChild(rel, s);
            return ProcCodeConstants.PROC_CODE_200S1000;
        }

        // 3：solution存在，但明细不存在情况不会出现

        // 方案及明细均存在，先删除旧的明细信息，再保存新的明细信息
        Solution s = ssApSolutionManager
                .load(rel.getSolutionId(), solutionType);
        if (null != s) {
            // 4-5: delete solutin and dtl, save solution and dtl
            List<SolutionRuleDtl> dlts = createRuleDtls(comDtlInfos, null,
                    solutionType, s.getMeta().getSolutionId());
            s.setRuleDtls(dlts);
            ssApSolutionManager.updateSolutionChild(s, solutionType);

            return ProcCodeConstants.PROC_CODE_200S1000;
        }

        return "方案不存在，无法操作！产品代码：" + acctProdCode;
    }

    @Override
    public String modifyGpRiskFundConfSolution(SsApSolution ssApSolution,
            String solutionType) {
        //To determine whether a solution is empty
        if (ssApSolution == null) {
            return "账户产品方案设置：数据为空";
        }

        String acctProdCode = ssApSolution.getProdCode();

        String error = checkProdCode(acctProdCode);
        if (null != error) {
            return error;
        }

        List<SolutionDtlInfo> rskDtlInfos = ssApSolution.getSolutionDtls();

        // 查询账户产品关系记录
        SsApSolutionRel rel = ssApSolutionManager.findRelByProdCode(
                acctProdCode, solutionType);

        // 如果请求参数为空，则表示删除此关系记录、方案记录及明细
        if (rskDtlInfos == null || rskDtlInfos.isEmpty()) {
            // 1: delete rel, delete solution, delete transDtl
            ssApSolutionManager.deleteRelAndChild(rel, solutionType);
            return ProcCodeConstants.PROC_CODE_200S1000;
        }

        // 如果请求参数存在有效数据，但关系表不存在对应记录，则新建关系记录，并保存新方案及明细信息
        if (rel == null) {
            // 2: save rel, save solution, save transDtl
            rel = createApSolRel(acctProdCode, solutionType);
            Solution s = new Solution();
            SolutionMeta meta = createSolutionMeta(solutionType);
            List<SolutionRuleDtl> dlts = createRuleDtls(rskDtlInfos, null,
                    solutionType, meta.getSolutionId());
            s.setMeta(meta);
            s.setRuleDtls(dlts);
            ssApSolutionManager.saveRelAndChild(rel, s);
            return ProcCodeConstants.PROC_CODE_200S1000;
        }

        // 3：solution存在，但明细不存在情况不会出现

        // 方案及明细均存在，先删除旧的明细信息，再保存新的明细信息
        Solution s = ssApSolutionManager
                .load(rel.getSolutionId(), solutionType);
        if (null != s) {
            // 4-5: delete solutin and dtl, save solution and dtl
            List<SolutionRuleDtl> dlts = createRuleDtls(rskDtlInfos, null,
                    solutionType, s.getMeta().getSolutionId());
            s.setRuleDtls(dlts);
            ssApSolutionManager.updateSolutionChild(s, solutionType);

            return ProcCodeConstants.PROC_CODE_200S1000;
        }

        return "方案不存在，无法操作！产品代码：" + acctProdCode;
    }

    /**
     * 查询风险金额度管理方案信息
     */
    @Override
    public SsApSolution findAPGpRiskFundSolutionByProdCode(String acctProdCode) {
        SsApSolutionRel rel = ssApSolutionManager.findRelByProdCode(
                acctProdCode, SolutionTypeConstant.GP_RISKFUND);
        if(rel !=null){
            
            SsApSolution aps = initSsApSolution(rel);
            setSsApSolutionDtlInfos(aps, rel.getSolutionType());
            return aps;
        }
        return null;
    }

    /**
     * 查询佣金额度管理方案信息
     */
    @Override
    public SsApSolution findAPGpComSolutionByProdCode(String acctProdCode) {
        SsApSolutionRel rel = ssApSolutionManager.findRelByProdCode(
                acctProdCode, SolutionTypeConstant.GP_COM);
        if (null != rel) {
            SsApSolution aps = initSsApSolution(rel);
            setSsApSolutionDtlInfos(aps,SolutionTypeConstant.GP_COM);
            return aps;
        }
        return null;
    }

    @Override
    public String modifyGpComSolution(SsApSolution ssApSolution) {
        return modifyGpComConfSolution(ssApSolution, SolutionTypeConstant.GP_COM);
    }

    @Override
    public String modifyGpRskSolution(SsApSolution ssApSolution) {
        return modifyGpRiskFundConfSolution(ssApSolution, SolutionTypeConstant.GP_RISKFUND);
    }

    @Override
    public SsApSolution findApProfitSolution(String acctProdCode,
            String gopayIntTxnCd) {
        SsApSolutionRel rel = ssApSolutionManager.findRelByProdCode(
                acctProdCode, SolutionTypeConstant.TRANS_PROFIT);
        if (null == rel) {
            return null;
        }
        SsApSolution aps = initSsApSolution(rel);
        setSsApSolutionDtlInfos(aps, rel.getSolutionType(), gopayIntTxnCd);
        return aps;
    }


    @Override
    public String modifyApProfitSolution(SsApSolution ssApSolution,
            String gopayIntTxnCd) {
        return modifyProdSolution(ssApSolution, gopayIntTxnCd,
                SolutionTypeConstant.TRANS_PROFIT);
    }

	@Override
	public SsApSolution findApSettleSolution(String acctProdCode,
			String gopayIntTxnCd) {
		SsApSolutionRel rel = ssApSolutionManager.findRelByProdCode(
                acctProdCode, SolutionTypeConstant.TRANS_SETTLE);
        if (null == rel) {
            return null;
        }
        SsApSolution aps = initSsApSolution(rel);
        setSsApSolutionDtlInfos(aps, rel.getSolutionType(), gopayIntTxnCd);
        return aps;
	}

	@Override
	public String modifyApSettleSolution(SsApSolution ssApSolution,
			String gopayIntTxnCd) {
		return modifyProdSolution(ssApSolution, gopayIntTxnCd,
                SolutionTypeConstant.TRANS_SETTLE);
	}

	
}